/*********************************************************
This imports data from Callfire from our May 2022 rool out

Last updated: May 15, 2025
*********************************************************/
clear all
cd "$data/2_field_experiment/may data/Callfire Spreadsheets"

//Creating variables that will allow date stamping files
local c_date = c(current_date)
local c_time = c(current_time)
local c_time_date = "`c_date'"+"_" +"`c_time'"
local time_string = subinstr("`c_time_date'", ":", "_", .)
local time_string = subinstr("`time_string'", " ", "_", .)

//
import delimited "export.csv", varnames(1) stringcols(1 7)

save ImportCallfireMay22`time_string', replace
clear

//should be 159 if 160 sheets
local i=1
foreach a of numlist 1/159 {
import delimited "export (`i').csv", varnames(1) stringcols(1 7)
capture confirm string variable link
if _rc {
    tostring link, replace force
}
append using ImportCallfireMay22`time_string' 
save ImportCallfireMay22`time_string', replace
local i=`i'+1
di `i'
clear
}


use ImportCallfireMay22`time_string' //Creates date stamped file of the imported Callfire data
gen Wave=4
save, replace

//----------Clean data------------//
duplicates drop //This will correct any duplicate downloads
unique tonumber //CHECK this should be 160 in full roll out // 20 messages x 2 phone numbers per message x 2 for male vs. female principal x 2 Roy vs. Erica first
		
//Remove phone calls from research team
*destring fromnumber, generate(num_fromnumber) force 
gen num_fromnumber = fromnumber
drop if num_fromnumber=="B0116871881" 

do "$scripts/CallFireMayJulyOverlap.do"
cd "$data/2_field_experiment/may data/Callfire Spreadsheets"
tostring num_fromnumber, replace force format(%20.0f)
merge m:1 num_fromnumber using DropDupEmails.dta
drop if _merge==3
drop if _merge==2
drop _merge

	//Below merges in 2 new variables; the treatment number 1 to 80 and the number of emails sent on May 24th 2022
	merge m:1 PhoneNumPair using "NoCallData.dta"
	drop _merge
	unique PhoneNumPair //should be 80 going to drop all this data and merge in later this is just a check
	tab treatment Variation
	tab treatment Treatment
	drop treatmentnumber treatment treatmentname N_treatmentnum 


save ImportCallfireMay22.dta, replace

//----------One Phone Call Quickly Cleaned
clear all
use "$data/2_field_experiment/may data/Callfire Spreadsheets/ImportCallfireMay22.dta"
keep if CallOrder==1 
save "$data/2_field_experiment/may data/ImportCallfireMay22_ONEcall.dta", replace
